package org.example.adb;

import java.io.BufferedReader;
import java.io.InputStreamReader;
//4. 列出设备上的文件
public class AdbListFilesExample {
    public static void main(String[] args) {
        //String remotePath = "/sdcard/Download";
        String remotePath = "/storage/emulated/0/DCIM";
        listFilesOnDevice(remotePath);
    }

    public static void listFilesOnDevice(String remotePath) {
        try {
            ProcessBuilder builder = new ProcessBuilder("adb", "shell", "ls", "-lh", remotePath);
            //ProcessBuilder builder = new ProcessBuilder("adb", "shell", "ls", "-m", remotePath);
            Process process = builder.start();

            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;

            System.out.println("Files in " + remotePath + ":");
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            process.waitFor();

            if (process.exitValue() != 0) {
                System.err.println("Error listing files.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
